﻿using System;
using System.Collections;
using System.Configuration;
using KPIS.GERP.GIMS.MODEL;
using KPIS.GERP.GIMS.DAL;
using Telerik.Web.UI;



namespace KPIS.GERP.GIMS.APP.APP_UI.Form
{
    public partial class ICM_Requisition : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        D_Requisition d_request = new D_Requisition();
        M_Requisition m_request = new M_Requisition();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnInsert_Item.OnClientClick = "openStautsAdd()";
                cmbStatusRequest.Text = "รอการอนุมัติ";
                LoadGrid();
                grdRequisition.DataBind();
                LoadToStatusCombo();
            }
        }
        protected void btnInsert_Click(object sender, EventArgs e)
        {

        }
        protected void grdRequisition_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            LoadGrid();
        }
        protected void grdRequisition_ItemCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "DataDelete")
                {
                    M_Requisition info = d_request.GetListFillBySeq(grdRequisition.Items[e.Item.ItemIndex]["requisition_code"].Text);
                    m_request.requisition_code = grdRequisition.Items[e.Item.ItemIndex]["requisition_code"].Text;
                    m_request.record_status = "D";
                    m_request.updated_by = cn.UserID;
                    m_request.updated_when = DateTime.Now;
                    d_request.Update_Requisition(m_request);
                }
            }
            catch (Exception ex)
            {
                RadAjaxManager1.Alert("เกิดข้อผิดพลาดขณะทำการลบข้อมูล");
                throw ex;
            }
            finally
            {
                LoadGrid();
                grdRequisition.Rebind();
            }
        }
        private void LoadGrid()
        {
            grdRequisition.DataSource = d_request.GetListForUserFillByEmpSeq(cmbStatusRequest.Text);
           // grdRequisition.DataBind();
        }
        protected void cmbStatusRequest_OnSelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadGrid();
            grdRequisition.Rebind();

            Int32 count = 0;
            count = grdRequisition.Items.Count;
            Int32 i;

            if (count > 0)
            {
                if ((cmbStatusRequest.Text == "รอการอนุมัติ") || (cmbStatusRequest.Text == "รายการตีกลับ"))
                {
                    grdRequisition.Items[0].Enabled = true;
                }
                else
                {
                    for (i = 0; i < count; i++)
                    {
                        grdRequisition.Items[i].Enabled = false;
                    }
                }
            }
        }
        private void LoadToStatusCombo()
        {
            RadComboBoxItem item0 = new RadComboBoxItem("รอการอนุมัติ", "");
            cmbStatusRequest.Items.Insert(0, item0);
            RadComboBoxItem item1 = new RadComboBoxItem("อนุมัติแล้ว", "1");
            cmbStatusRequest.Items.Insert(1, item1);
            RadComboBoxItem item2 = new RadComboBoxItem("รายการตีกลับ", "2");
            cmbStatusRequest.Items.Insert(2, item2);
            RadComboBoxItem item3 = new RadComboBoxItem("รายการทั้งหมด", "3");
            cmbStatusRequest.Items.Insert(3, item3);
        }
        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            grdRequisition.DataSource = d_request.GetListForUserFillByEmpSeq (cmbStatusRequest.Text);
            grdRequisition.Rebind();
        }
    }
}
